#ifndef OMG_DDS_DATAWRITER_HPP_
#define OMG_DDS_DATAWRITER_HPP_

/* Copyright (c) 2009-2010, Real-Time Innovations, Inc.
 * Copyright (c) 2010, Object Management Group, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - Neither the names of the above copyright holders nor the names of their
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL A COPYRIGHT HOLDER OR CONTRIBUTOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


template <typename DELEGATE>
class tdds::pub::DataWriterBase : public dds::core::Entity
{
    OMG_DDS_REFERENCE_TYPE(DataWriterBase)

public:
    void wait_for_acknowledgments(const dds::core::Duration &max_wait);

    const dds::pub::LivelinessLostStatus get_liveliness_lost_status();

    const dds::pub::OfferedDeadlineMissedStatus
    get_offered_deadline_missed_status();

    const dds::pub::OfferedIncompatibleQosStatus
    get_offered_incompatible_qos_status();

    const dds::pub::PublicationMatchedStatus get_publication_matched_status();

    void assert_liveliness();

    dds::core::InstanceHandleSeq&
    get_matched_subscriptions(dds::core::InstanceHandleSeq& subscription_handles) const;

    dds::core::InstanceHandleSeq
    get_matched_subscriptions() const;

    const dds::domain::discovery::SubscriptionBuiltinTopicData
    get_matched_subscription_data(
        const dds::core::InstanceHandle& subscription_handle);

    // --- Entity methods: --- //
public:
    /**
     * This method closes the entity and releases all resources associated with
     * DDS, such as threads, sockets, buffers, etc. Any attempt to invoke
     * methods on a closed entity will raise an AlreadyClosedError.
     */
    virtual void close();

    using dds::core::Entity::set_qos;
    void set_qos(const dds::pub::DataWriterQos& qos);
    const dds::pub::DataWriterQos get_qos() const;

    dds::pub::Publisher get_parent() const;

    // --- State: --- //
protected:
    friend class OMG_DDS_DATA_WRITER_DELEGATE;
    DELEGATE impl_;
};


template <typename T> 
class dds::pub::DataWriter : public DataWriterBase
{
    OMG_DDS_REFERENCE_TYPE(DataWriter)

public:
    typedef dds::core::StatusCondition< DataWriter<T> > StatusConditionType;
public:
    typedef T DataType;

public:
    dds::topic::Topic<T> get_topic();

public:
    const dds::core::InstanceHandle register_instance(const T& instance_data);
    const dds::core::InstanceHandle register_instance(const T& instance_data,
                                                      const dds::core::Time& source_timestamp) ;

    void unregister_instance(const T& instance_data, 
                             const dds::core::InstanceHandle& handle);
    void unregister_instance(const T& instance_data, 
                             const dds::core::InstanceHandle& handle,
                             const dds::core::Time& source_timestamp);
    
    void write(const T& instance_data);
    void write(const T& instance_data,
               const dds::core::Time& source_timestamp);
    void write(const T& instance_data, 
               const dds::core::InstanceHandle& handle);
    void write(const T& instance_data, 
               const dds::core::InstanceHandle& handle, 
               const dds::core::Time& source_timestamp);
    
    void dispose(const T& instance_data, 
                 const dds::core::InstanceHandle& handle);
    void dispose(const T& instance_data, 
                 const dds::core::InstanceHandle& handle,
                 const dds::core::Time& source_timestamp);

    T& get_key_value(T& key_holder, const dds::core::InstanceHandle& handle) const;
    T get_key_value(const dds::core::InstanceHandle& handle) const;

    const dds::core::InstanceHandle lookup_instance(const T& key_holder);

    // --- Entity methods: --- //
public:
    StatusConditionType get_statuscondition();

    void set_listener(dds::pub::DataWriterListener<T>* l,
                      const dds::core::StatusMask& mask = dds::core::StatusMask::all());

    dds::pub::DataWriterListener<T>* get_listener() const;
};


#endif // !defined(OMG_DDS_DATAWRITER_HPP_)
